//
//  YZMPickerView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/11.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMPickerView.h"
#import "YZMSeparatorCell.h"
#import "YZMPopupSheet.h"

@interface YZMPickerViewCell : YZMSeparatorCell

@end

@implementation YZMPickerViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.textColor = [YZMAppStyle colorForGrayText];
        self.textLabel.font = [YZMAppStyle mediumSizeFont];
        self.leftMargin = 0.0;
        self.rightMargin = 0.0;
    }
    return self;
}

@end

static const float pay_way_choose_view_height_for_cell = 44.0;
static NSString *const sYZMPickerViewCellID = @"YZMPickerViewCellID";

@interface YZMPickerView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation YZMPickerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self createObjects];
    }
    return self;
}

#pragma mark -- UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YZMPickerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sYZMPickerViewCellID forIndexPath:indexPath];
    cell.textLabel.text = self.dataSource[indexPath.row];
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row;
    indexCount.count = self.dataSource.count;
    cell.indexCount = indexCount;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_dataSource count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return pay_way_choose_view_height_for_cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectedAction) {
        
        self.selectedAction(self.dataSource[indexPath.row]);
    }
    
    [YZMPopupSheet dismissForContentView:self];
}

- (void)createObjects
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1, 1) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [_tableView registerClass:[YZMPickerViewCell class] forCellReuseIdentifier:sYZMPickerViewCellID];
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.left.right.equalTo(self);
        maker.height.mas_equalTo(0);
        maker.bottom.equalTo(self).offset(-20);
    }];
}

- (void)setDataSource:(NSArray *)dataSource{
    
    _dataSource = dataSource;
    [_tableView mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.height.mas_equalTo(_dataSource.count * pay_way_choose_view_height_for_cell);
    }];
    [_tableView reloadData];
}


@end
